<template>
	<view>
		<!-- tabbar -->
		<uni-nav-bar title="资讯订阅" @clickLeft="back" left-icon="left" backgroundColor="#fff" color="#000"
			statusBar="true" />
		<view class="main">
			<scroll-view class="scrollheight" scroll-y="true" :style="{height:setcollapseheight() + 'px'}"
				@scrolltolower="handleScrolltolowerone()">
				<view class="item" v-for="(item,index) in messageSubscriptionList" :key="index">
					<view class="left">
						<image class="img" :src="imgList[index]"></image>
						<view class="text">
							{{item.subscriptionTypeName}}
						</view>
					</view>
					<view class="right">
						<template v-if="index === 0">
							<template v-if="!item.subscribeFlag">
								<u-button class="btn" size="mini" text="去选择" @click="goSelectArable(item.subscriptionType,1)"></u-button>
							</template>
							<template v-else>
								<u-button class="btn btn1" size="mini" text="继续订阅" @click="goSelectArable(item.subscriptionType,2)"></u-button>
								<u-button class="btn" size="mini" text="取消订阅" @click="unsubscribe(item.subscriptionType)"></u-button>
							</template>
						</template>
						<template v-else>
							<u-button class="btn" v-if="!item.subscribeFlag"  size="mini" text="点击订阅" @click="subscribe(item.subscriptionType)"></u-button>
							<u-button class="btn"  v-else size="mini" text="取消订阅" @click="unsubscribe(item.subscriptionType)"></u-button>
						</template>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import API from '@/api/socialized-servce/farm-service/user/messageSubscription/index.js'
	export default {
		data() {
			return {
				messageSubscriptionList: [],
				serviceProviderNo: '',
				selectEquipmentNo: [],
				imgList: [
					require('@/static/socialized-service/farm-service/messageSubscription/1.png'),
					require('@/static/socialized-service/farm-service/messageSubscription/2.png'),
					require('@/static/socialized-service/farm-service/messageSubscription/3.png'),
					require('@/static/socialized-service/farm-service/messageSubscription/4.png'),
					require('@/static/socialized-service/farm-service/messageSubscription/5.png')
				],
			}
		},
		mounted() {
			this.serviceProviderNo = uni.getStorageSync('serviceProviderNo')
			this.querySubscribe()
		},
		methods: {
			async querySubscribe() {
				let params = {
					serviceProviderNo: this.serviceProviderNo
				}
				let res = await API.querySubscribe(params)
				if(res.code === 200) {
					this.messageSubscriptionList = res.data
					this.messageSubscriptionList = this.messageSubscriptionList.filter(el=>el !==null)
					console.log(this.messageSubscriptionList);
					let arr = res.data.filter(el => el.subscriptionType === 'DEVICE')
					if(arr.length>0) {
						if(arr[0].subscribeFlag) {
							this.selectEquipmentNo = arr[0].val
						}
					}
				}
			},
			async subscribe(type) {
				let params = {
					subscriptionType: type,
					serviceProviderNo: this.serviceProviderNo
				}
				let res = await API.subscribe(params)
				if(res.code === 200) {
					this.querySubscribe()
				}
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			},
			async unsubscribe(type) {
				let params = {
					subscriptionType: type,
					serviceProviderNo: this.serviceProviderNo
				}
				let res = await API.unsubscribe(params)
				if(res.code === 200) {
					this.selectEquipmentNo = []
					this.querySubscribe()
				}
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			},
			setcollapseheight() {
				let topheightone = uni.upx2px(260)
				let newheight = this.customBar + topheightone
				let winHeight = 0
				uni.getSystemInfo({
					success: function(res) {
						winHeight = res.windowHeight
					}
				});
				winHeight = parseInt(winHeight) - topheightone
				// #ifndef H5
				winHeight = winHeight

				// #endif
				return winHeight
			},
			back() {
				uni.reLaunch({
					url: '/subPage1/socialized-service/farm-service/normalUser/serviceSelect/serviceClassify'
				})
			},
			goSelectArable(type,status) {
				uni.setStorageSync('selectEquipmentNo',this.selectEquipmentNo)
				uni.redirectTo({
					url: `/subPage1/socialized-service/farm-service/user/selectEnvironmental/index?&status=${status}&subscriptionType=${type}`
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 48rpx 36rpx 0 36rpx;
		.item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 16rpx;
			padding-right: 32rpx;
			height: 104rpx;
			background: #FFFFFF;
			border-radius: 10rpx 10rpx 10rpx 10rpx;
			opacity: 1;
			border: 2rpx solid #EDEDED;
			
			.left {
				display: flex;
				align-items: center;
				
				.img {
					width: 92rpx;
					height: 86rpx;
				}
				
				.text {
					margin-left: 34rpx;
					font-size: 28rpx;
					font-weight: 600;
					color: #434343;
				}
			}
			
			.right {
				display: flex;
				.btn {
					width: 156rpx;
					height: 58rpx;
					border: 2rpx solid #ccc;
					padding: 24rpx!important;
					background: linear-gradient(270deg, #88ACFF 0%, #2165FF 100%);
					box-shadow: 0rpx 0rpx 8rpx 2rpx rgba(0,0,0,0.16);
					border-radius: 30rpx 30rpx 30rpx 30rpx;
					color: #fff;
					font-size: 28rpx;
				}
				.btn1 {
					margin-right: 20rpx;
				}
			}
		}
		:nth-child(1) {
			margin-top: 0;
		}
	}
	
	/deep/ .u-button__text {
		font-size: 28rpx!important;
		font-weight: normal;
	}
</style>